package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IDispatchRuleConfigurationService;
import com.mdd.admin.validate.DispatchRuleConfigurationCreateValidate;
import com.mdd.admin.validate.DispatchRuleConfigurationUpdateValidate;
import com.mdd.admin.validate.DispatchRuleConfigurationSearchValidate;
import com.mdd.admin.vo.DispatchRuleConfigurationListedVo;
import com.mdd.admin.vo.DispatchRuleConfigurationDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.DispatchRuleConfiguration;
import com.mdd.common.mapper.DispatchRuleConfigurationMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 发文规则配置实现类
 * @author LikeAdmin
 */
@Service
public class DispatchRuleConfigurationServiceImpl implements IDispatchRuleConfigurationService {
        
    @Resource
    DispatchRuleConfigurationMapper dispatchRuleConfigurationMapper;

    /**
     * 发文规则配置列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<DispatchRuleConfigurationListedVo>
     */
    @Override
    public PageResult<DispatchRuleConfigurationListedVo> list(PageValidate pageValidate, DispatchRuleConfigurationSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DispatchRuleConfiguration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        dispatchRuleConfigurationMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:customerName@customer_name:str",
        });

        IPage<DispatchRuleConfiguration> iPage = dispatchRuleConfigurationMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DispatchRuleConfigurationListedVo> list = new LinkedList<>();
        for(DispatchRuleConfiguration item : iPage.getRecords()) {
            DispatchRuleConfigurationListedVo vo = new DispatchRuleConfigurationListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 发文规则配置详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return DispatchRuleConfiguration
     */
    @Override
    public DispatchRuleConfigurationDetailVo detail(Integer id) {
        DispatchRuleConfiguration model = dispatchRuleConfigurationMapper.selectOne(
                new QueryWrapper<DispatchRuleConfiguration>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        DispatchRuleConfigurationDetailVo vo = new DispatchRuleConfigurationDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 发文规则配置新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(DispatchRuleConfigurationCreateValidate createValidate) {
        DispatchRuleConfiguration model = new DispatchRuleConfiguration();
        model.setCustomerName(createValidate.getCustomerName());
        model.setBlindCarbonCopy(createValidate.getBlindCarbonCopy());
        model.setCaseType(createValidate.getCaseType());
        model.setDocumentType(createValidate.getDocumentType());
        model.setThemeId(createValidate.getThemeId());
        model.setTheme(createValidate.getTheme());
        model.setRecipientsId(createValidate.getRecipientsId());
        model.setRecipients(createValidate.getRecipients());
        model.setCopy(createValidate.getCopy());
        model.setFileDesignationType(createValidate.getFileDesignationType());
        dispatchRuleConfigurationMapper.insert(model);
    }

    /**
     * 发文规则配置编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(DispatchRuleConfigurationUpdateValidate updateValidate) {
        DispatchRuleConfiguration model = dispatchRuleConfigurationMapper.selectOne(
                new QueryWrapper<DispatchRuleConfiguration>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setBlindCarbonCopy(updateValidate.getBlindCarbonCopy());
        model.setCaseType(updateValidate.getCaseType());
        model.setDocumentType(updateValidate.getDocumentType());
        model.setThemeId(updateValidate.getThemeId());
        model.setTheme(updateValidate.getTheme());
        model.setRecipientsId(updateValidate.getRecipientsId());
        model.setRecipients(updateValidate.getRecipients());
        model.setCopy(updateValidate.getCopy());
        model.setFileDesignationType(updateValidate.getFileDesignationType());
        dispatchRuleConfigurationMapper.updateById(model);
    }

    /**
     * 发文规则配置删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        DispatchRuleConfiguration model = dispatchRuleConfigurationMapper.selectOne(
                new QueryWrapper<DispatchRuleConfiguration>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        dispatchRuleConfigurationMapper.updateById(model);
    }

}
